# -*- coding:utf-8 -*-

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

from src.com.itheima.web.util.base_util import BaserUtil


class SeleniumUtil():
    def __init__(self, driver=None,headless_flag=False,load_selenium=False):
        if load_selenium==True:
            if driver==None:
                driver = self.get_defaule_driver(headless_flag)
            self.driver=driver
            self.com_client=BaserUtil()


    def get_defaule_driver(self,headless_flag):
        chrome_options = Options()
        if headless_flag:
            chrome_options.add_argument("--headless")
        chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
        # 禁用自动化标志
        chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
        chrome_options.add_experimental_option('useAutomationExtension', False)

        driver = webdriver.Chrome(options=chrome_options)
        driver.maximize_window()
        return driver

    def open_url(self,URL,driver=None,fast_falg=False):
        if driver==None:
            driver=self.driver
        driver.get(URL)
        # 等待body加载完成
        wait = WebDriverWait(driver, 10)
        wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
        if not fast_falg:
            self.com_client.sleep_period_time(min_value=3, max_value=6)
        return driver

    def move_and_clink(self,element,driver=None,fast_falg=False):
        if driver==None:
            driver=self.driver
        # 将逛遍移动到可视区域
        driver.execute_script("arguments[0].scrollIntoView(true);", element)
        # 创建ActionChains对象
        actions = ActionChains(driver)
        # 将光标移动到元素上
        actions.move_to_element(element)
        # 执行点击动作
        actions.click()
        # 执行所有的操作
        actions.perform()
        WebDriverWait(driver, 10)
        if not fast_falg:
            self.com_client.sleep_period_time(min_value=3, max_value=6)
        return driver

    def move_and_clink_xpath(self,xpath,driver=None,fast_falg=False,index=0):
        element = driver.find_elements(By.XPATH, xpath)[index]
        return self.move_and_clink(element=element,driver=driver,fast_falg=fast_falg)

    def find_element(self,xpath,driver=None,index=0):
        elements = self.find_elements(xpath=xpath, driver=driver)
        return elements[index]

    def find_elements(self, xpath, driver=None):
        if driver == None:
            driver = self.driver
        elements = driver.find_elements(By.XPATH, xpath)
        return elements

    def select_element_xpath(self, xpath, driver=None,fast_falg=False,index=0):
        select_box = self.find_element(xpath=xpath, driver=driver, index=0)
        select = Select(select_box)
        select.select_by_index(index)
        WebDriverWait(driver, 10)
        if not fast_falg:
            self.com_client.sleep_period_time(min_value=3, max_value=6)
        return driver

    def switch_frame(self,iframe,driver=None,fast_falg=False):
        if driver == None:
            driver = self.driver
        driver.switch_to.frame(iframe)
        WebDriverWait(driver, 10)
        if not fast_falg:
            self.com_client.sleep_period_time(min_value=3, max_value=6)
        return driver

    def source_code(self, driver=None):
        if driver == None:
            driver = self.driver
        # 等待body加载完成
        wait = WebDriverWait(driver, 10)
        wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
        driver_page_source = driver.page_source
        return driver_page_source


if __name__ == '__main__':
    this=SeleniumUtil(load_selenium=True,headless_flag=False,driver=None)
    url='https://blog.csdn.net/weixin_43767015/article/details/109842116'
    this.open_url(URL=url)

